﻿/********************************************************
 * Module Name    : PO Delete
 * Purpose        : To delete various attachments/chats/etc. of a record
 * Class Used     : ---
 * Chronological Development
 * Veena Pandey     -Feb-2009
 ******************************************************/

using System;
//using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data;
using VAdvantage.DataBase;
using VAdvantage.Logging;

namespace VAdvantage.Model
{
    /// <summary>
    /// To delete various attachments/chats/etc. of a record
    /// </summary>
    class PORecord
    {

        private static VLogger log = VLogger.GetVLogger(typeof(PORecord).FullName);

        //	Parent Table ID
        private static int[] _parents = new int[]{
        X_C_Order.Table_ID,
        X_CM_Container.Table_ID
        };

        //	Parent Table Names
        private static string[] _parentNames = new string[]{
        X_C_Order.Table_Name,
        X_CM_Container.Table_Name
        };

        // Child Table ID
        private static int[] _parentChilds = new int[]{
        X_C_OrderLine.Table_ID,
        X_CM_Container_Element.Table_ID
        };

        // Child Table Names
        private static string[] _parentChildNames = new string[]{
        X_C_OrderLine.Table_Name,
        X_CM_Container_Element.Table_Name
        };

        //	Cascade Table ID
        private static int[] _cascades = new int[]{
        X_AD_Attachment.Table_ID,
        X_AD_Archive.Table_ID,
        X_K_Index.Table_ID,
        X_AD_Note.Table_ID,
        X_MailAttachment1.Table_ID,
        X_AppointmentsInfo.Table_ID

        };

        //	Cascade Table Names
        private static string[] _cascadeNames = new string[]{
        X_AD_Attachment.Table_Name,
        X_AD_Archive.Table_Name,
        X_K_Index.Table_Name, 
        X_AD_Note.Table_Name,
        X_MailAttachment1.Table_Name,
        X_AppointmentsInfo.Table_Name
        };

        //	Restrict Table ID
        private static int[] _restricts = new int[]{
        X_R_Request.Table_ID,
        X_CM_Chat.Table_ID
        };

        //	Restrict Table Names
        private static string[] _restrictNames = new string[]{
        X_R_Request.Table_Name,
        X_CM_Chat.Table_Name
        };

        /// <summary>
        /// Delete Cascade including (selected)parent relationships
        /// </summary>
        /// <param name="AD_Table_ID">table id</param>
        /// <param name="Record_ID">record id</param>
        /// <param name="trxName">transaction object</param>
        /// <returns>false if could not be deleted</returns>
        /// <author>Veena Pandey</author>
        public static bool DeleteCascade(int AD_Table_ID, int Record_ID, string trxName)
        {
            bool deleting = true;
            var client = Utility.AEnv.GetDataServiceBasic2Client();
            client.DeletePOCascadeCompleted += (s, e) =>
                {
                    if (e.error != null || e.Error != null)
                    {
                        //log
                    }
                    else
                    {
                        System.Collections.Generic.List<String> list = e.Result;
                        for (int i = 0; i < list.Count; i++)
                        {
                            log.Info(list[i]);
                        }
                    }
                    deleting = false;
                };
            client.DeletePOCascadeAsync(AD_Table_ID, Record_ID, trxName);
            client.CloseAsync();
            while (deleting)
            {
                System.Threading.Thread.Sleep(1);
            }
            return true;
        }

        /// <summary>
        /// An entry Exists for restrict table/record combination
        /// </summary>
        /// <param name="AD_Table_ID">table id</param>
        /// <param name="Record_ID">record id</param>
        /// <param name="trxName">transaction</param>
        /// <returns>error message (Table Name) or null</returns>
        /// <author>Veena Pandey</author>
        public static string Exists(int AD_Table_ID, int Record_ID, string trxName)
        {
            //	Table Loop only
            for (int i = 0; i < _restricts.Length; i++)
            {
                //	SELECT COUNT(*) FROM table WHERE AD_Table_ID=#1 AND Record_ID=#2
                StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM ")
                    .Append(_restrictNames[i])
                    .Append(" WHERE AD_Table_ID=" + AD_Table_ID + " AND Record_ID=" + Record_ID);
                int no = Utility.Util.GetValueOfInt(DB.ExecuteScalar(sql.ToString(), null, trxName));
                if (no > 0)
                    return _restrictNames[i];
            }
            return null;
        }
    }
}
